package com.heima.article.service.imp;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.ArticleConstans;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author：shi_xin_rui
 * @name：ApArticleServiceImpl
 * @Date：2023/6/3 15:13
 */
@Slf4j
@Transactional
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Override
    public ResponseResult loadArticle(Short loadType, ArticleHomeDto dto) {
        Integer size = dto.getSize();
        // 校验size
        if(size == 0 || size == null || size > 50){
            dto.setSize(ArticleConstans.DEFAULT_SIZE);
        }
        // 校验tag
        String tag = dto.getTag();
        if (tag == null || "".equals(tag)){
            dto.setTag(ArticleConstans.DEFAULT_TAG);
        }
        // 校验时间
        Date maxBehotTime = dto.getMaxBehotTime();
        Date minBehotTime = dto.getMinBehotTime();
        if (maxBehotTime == null){
            dto.setMaxBehotTime(new Date());
        }
        if (minBehotTime == null){
            dto.setMinBehotTime(new Date());
        }
        List<ApArticle> articleList = apArticleMapper.loadArticle(loadType, dto);
        return ResponseResult.okResult(articleList);
    }
}
